<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

/*
| -------------------------------------------------------------------
| Login
| -------------------------------------------------------------------
| Uses selected Authenticator to authenticate. If successful, creates
| a CodeIgniter session.
|
| Successful login redirects to defined landing page.
| Unsuccessful login redirects to auth_failed.
|
|
*/

/* include(APPPATH.'libraries/auth/Authenticator.php');  */

class Login extends CI_Controller {

	function __construct()
	{
		parent::__construct();	

	}

	function index()
	{
		$this->load->helper('url_helper');
		$this->load->helper('form_helper');
		$this->load->library('session');
        $this->config->load('auth/AuthenticatedControllerConfig');
		$authenticatorclass = $this->config->item('authenticator');
        $this->load->library($authenticatorclass,'','authenticator');

	    if ( $this->authenticator->login(   $this->input->post('username'),
	                                        $this->input->post('password') ) )
	    {
						if (strlen($this->input->get_post('url')) > 0)
						{
							redirect(urldecode( $this->input->get_post('url') ), 'location');
						}
						else
						{
							$groups = $this->session->userdata('groups');
							$landing = $this->config->item('landing');
							if (!$landing)
							{
								print "Error no landing specified for $groups[0]\n";
							}
							redirect($landing[$groups[0]], 'location');
						}
			}
			else
			{
					redirect($this->config->item('auth_failed'), 'location');
			}
	}
    	
}
